package com.android.BluethootChess.rules;

import java.util.List;

import android.graphics.Point;

import com.android.BluethootChess.model.GameBoardMatrix;
import com.android.BluethootChess.model.PieceColor;
import com.android.BluethootChess.util.Utils;

/**
 * Clasa abstracta pentru a lucra cu regulile pieselor de pe tabla de sah.
 */
public abstract class PieceRule {

	protected byte[][] matrix;
	protected Point piecePosition;
	protected PieceColor pieceColor;

	public PieceRule(GameBoardMatrix gameMatrix, PieceColor pieceColor) {
		this.matrix = gameMatrix.getGameBoard();
		this.pieceColor = pieceColor;
	}

	/**
	 * Verifica daca se poate muta la coordonatele x,y.
	 * 
	 * @param x
	 *            Coordonata de pe axa X
	 * @param y
	 *            Coordonata de pe axa Y
	 * @return True daca piesa se poate muta, in alt caz altfel
	 */
	public boolean canMove(int x, int y) {
		List<Point> avaiblePoints = findAllAvaiblePositions();
		if (avaiblePoints != null) {
			return avaiblePoints.contains(new Point(x, y));
		}
		return false;
	}

	/**
	 * Gaseste toate pozitiile in care o piesa poate fi mutata.
	 * 
	 * @return Lista ce contine punctele la care poate fi mutata piesa
	 */
	public abstract List<Point> findAllAvaiblePositions();

	/**
	 * Seteaza piese la pozitia dorita.
	 * 
	 * @param position
	 *            Pozitia la care va fi setata piesa
	 */
	public void setPiecePosition(Point position) {
		piecePosition = position;
	}

	public Point getpiecePosition() {
		return piecePosition;
	}

	/**
	 * Verifica daca coordonatele se afla pe tabla.
	 * 
	 * @param x
	 *            - coordonata x
	 * @param y
	 *            - coordonata y
	 * @return true daca punctul (x,y) apartine matricei, false in caz contrar.
	 * */
	protected boolean isValidLocation(int x, int y) {
		if (x < 0 || y < 0) {
			return false;
		}
		boolean boundTest = (x < matrix.length && y < matrix.length);
		if (boundTest == false) {
			return false;
		}
		boolean colorTest = (Utils.getPieceColor(matrix[x][y]) != pieceColor || matrix[x][y] == -1);
		return boundTest && colorTest;
	}

}
